<?php

namespace App\Admin\Controllers;

use App\Admin\Repositories\WxWallpaper;
use App\Wen\Utils\Utils;
use App\Models\User\WxUser;
use App\Models\WxWallpapersSubject;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Show;
use Dcat\Admin\Http\Controllers\AdminController;

class WxWallpaperController extends AdminController
{
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(new WxWallpaper(), function (Grid $grid) {
            $grid->quickSearch(['id', 'subject_id','user_id'])->placeholder('搜索图集ID，主题id，标题，作者id...')->width(50);
            $grid->model()->whereNull('bureau_id');
            $grid->column('id')->sortable();
            $grid->column('subject_id');
            $grid->column('imgs')->display(function ($v) {
                if(_empty_($v)){
                    return '图集里没有图片';
                }
                $imgs = json_decode($v, true);
                $html = '<div style="max-width: 350px; overflow-x: scroll;">';
                foreach ($imgs as $img){
                    $html .= '<img data-action="preview-img" style="max-width: 50px;margin-right: 10px;" src="'.Utils::imgWithStyle($img).'" / >';
                }
                return $html.'</div>';
            })->width('50px');

            $grid->column('title');
            $grid->column('user_id');
//            $grid->column('created_at');
            $grid->column('updated_at')->sortable();

            $grid->filter(function (Grid\Filter $filter) {
                $filter->equal('id');

            });
        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, new WxWallpaper(), function (Show $show) {
            $show->field('id');
            $show->field('subject_id');
            $show->field('imgs');
            $show->field('title');
            $show->field('user_id');
            $show->field('created_at');
            $show->field('updated_at');
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new WxWallpaper(), function (Form $form) {
            $form->display('id');
            $form->select('subject_id')->required()
                ->options(WxWallpapersSubject::pluck('name','id'));
            $form->multipleImage('imgs')->required()->url('files/uploads')->uniqueName()->autoUpload()->limit(100)->help('看你主要适配的机型，图片大致跟手机屏幕比例接近即可')->saveAsJson();
            $form->text('title')->required();
//            $form->select('user_id')->required()->options(WxUser::limit(10)->pluck('user_name','id'));
            $form->text('user_id')->required();
            $form->display('created_at');
            $form->display('updated_at');
            $form->deleting(function (Form $form){
                global $__MINI_GLOBAL_TENANT_ID__;
                if($__MINI_GLOBAL_TENANT_ID__ > 0){
                    return $form->response()->error('权限不足，不可以删除其他分站对象');
                }
            });
        });
    }
}
